package com.neuedu.wemall.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.neuedu.wemall.dao.entity.Category;
import com.neuedu.wemall.service.ICategoryService;
import com.neuedu.wemall.dao.mapper.CategoryMapper;

import java.util.Date;
import java.util.List;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> getCategoryList() {
        //查询一级分类
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", 1);
        queryWrapper.eq("del_status", 1);
        queryWrapper.orderByAsc("sort");
        List<Category> categoryList = getBaseMapper().selectList(queryWrapper);
        if (CollectionUtil.isEmpty(categoryList)) {
            return null;
        }
        //查询二级分类
        for (Category category : categoryList) {
            QueryWrapper<Category> qw = new QueryWrapper<>();
            qw.eq("parent_id", category.getId());
            qw.eq("del_status", 1);
            qw.orderByAsc("sort");
            List<Category> children = getBaseMapper().selectList(qw);
            category.setChildren(children);
        }
        return categoryList;
    }

    @Override
    public int insertOne(Category category) {
        String name = category.getName();
        Integer parentId = category.getParentId();
        //判断是否存在相同名称的分类.1.一级分类名称不可以重复;2.同一个一级分类中不可以包含相同的二级分类名称
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getName, category.getName());
        int level;
        if (parentId != null) {
            level = 2;
            lqw.eq(Category::getParentId, parentId);
        } else {
            level = 1;
            lqw.isNull(Category::getParentId);
        }
        int count = getBaseMapper().selectCount(lqw).intValue();
        if (count > 0) {
            return 2;
        }
        category.setSort(0);
        category.setLevel(level);
        category.setDelStatus(1);
        category.setCreateTime(new Date());
        int rows = baseMapper.insert(category);
        return rows;
    }

    @Override
    public int updateOneById(Category category) {
        String name = category.getName();
        if (StrUtil.isNotEmpty(name)) {
            //如果修改了商品分类名称,判断修改后的商品分类名称是否存在或重复
            Integer parentId = category.getParentId();
            LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Category::getName, name);
            lqw.ne(Category::getId, category.getId());
            if (parentId == null) {
                lqw.isNull(Category::getParentId);
            } else {
                lqw.eq(Category::getParentId, parentId);
            }
            int count = getBaseMapper().selectCount(lqw).intValue();
            if (count > 0) {
                return 2;
            }
        }
        Integer level = category.getLevel();
        if (!(level == null || 1 == level || 2 == level)) {
            throw new IllegalArgumentException("商品分类等级错误");
        }
        Integer delStatus = category.getDelStatus();
        if (!(delStatus == null || 1 == delStatus || 9 == delStatus)) {
            throw new IllegalArgumentException("商品分类删除状态错误");
        }
        category.setModifyTime(new Date());
        int rows = baseMapper.updateById(category);
        return rows;
    }

    @Override
    public Page<Category> getPageList(int pageNum, int pageSize, String keyword, Category category) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            lqw.and(wrapper -> {
                wrapper.like(Category::getName, keyword);
                wrapper.or();
                wrapper.like(Category::getDesc, keyword);
            });
        }
        Integer level=category.getLevel();
        if(level!=null && 0!=level){
            lqw.eq(Category::getLevel,level);
        }
        Integer delStatus=category.getDelStatus();
        if(delStatus!=null && 0!=delStatus){
            lqw.eq(Category::getDelStatus,delStatus);
        }
        Page<Category> page=new Page<>(pageNum,pageSize);
        return getBaseMapper().selectPage(page,lqw);
    }
}